from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType 


class UserImage(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey("content_type", "object_id")

    user = models.ForeignKey(User)
    path = models.CharField(max_length=100)
    size = models.PositiveIntegerField(default=0)
    submit_date = models.DateTimeField(auto_now=True)
    ip_address = models.IPAddressField(blank=True, null=True)

    class Meta:
        ordering = ('submit_date',)

    def __unicode__(self):
        return self.path


class NullObjManager(models.Manager):

    cacheobj = None
    def getcacheobj(self):
        if self.cacheobj: return self.cacheobj

        try:
            self.cacheobj = self.get(pk=1)
        except:
            self.cacheobj = self.model()
            self.cacheobj.save()

        return self.cacheobj


class NullObj(models.Model):
    ts = models.DateTimeField(auto_now=True)
    objects = NullObjManager()



